package com.itqn.service.Impl;

import com.itqn.dao.CategoryDao;
import com.itqn.dao.Impl.CategoryDaoImpl;
import com.itqn.pojo.Category;
import com.itqn.pojo.PageBean;
import com.itqn.service.CategoryService;
import com.itqn.service.ProductService;

import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    CategoryDao categoryDao=new CategoryDaoImpl();

    @Override
    public int deleteCategory(int id) throws Exception {
        return categoryDao.deleteCategory(id);
    }

    @Override
    public int AddCategory(Category category) throws Exception {
        return categoryDao.AddCategory(category);
    }

    @Override
    public Category findCategoryById(int id) throws Exception {
        return categoryDao.findCategoryById(id);
    }

    @Override
    public int updateCategory(Category category) throws Exception {
        return categoryDao.updateCategory(category);
    }

    @Override
    public PageBean<Category> query(String name, Integer pageSize, Integer page) throws Exception {
        PageBean pb = new PageBean();
        // 封装商品集合
        pb.setList(categoryDao.query(name, pageSize, page));
        // 封装总条数
        Integer totalCount = categoryDao.totalCount(name, pageSize, page);
        pb.setTotalCount(totalCount);
        // 封装总页数
        pb.setPageCount(totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1));
        return pb;
    }

    @Override
    public Integer updatestatic(Category category) throws Exception {

        ProductService productService = new ProductServiceImpl();
        Integer count = productService.count(category.getId());
        if (category.getStatus() == 0 && count > 0) {
            return 400;
        }
        return categoryDao.updatestatic(category);
    }

    @Override
    public List<Category> findAllCategory() throws Exception {
        return categoryDao.findAllCategory();
    }
}
